package com.laxcen.smoke.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.config.CommonEnum;
import com.laxcen.smoke.dao.OrderDao;
import com.laxcen.smoke.dto.res.OrderAllListRes;
import com.laxcen.smoke.dto.res.OrderListRes;
import com.laxcen.smoke.dto.res.OrderRes;
import com.laxcen.smoke.service.OrderService;
import com.laxcen.smoke.util.DBUtils;
import com.laxcen.smoke.util.Response;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Value("${accessKey}")
    private String accessKey;

    @Value("${accessSecret}")
    private String accessSecret;

    @Value("${productkey}")
    private String ProductKey;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Response<PageInfo<OrderAllListRes>> getOrderList(int pageNo, int pageSize, int roleflag, String province, String city, String area) {
        Page page = PageHelper.startPage(pageNo,pageSize);
        List<OrderAllListRes> orderAllListRes = new LinkedList<>();
        if (roleflag==0) {
            List<OrderRes> list = orderDao.getOrderList();
            String[] boxnolist = new String[list.size()];
            String[] optypelist = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                String boxno = list.get(i).getBoxno();
                boxnolist = boxno.split(",");
                String optype = String.valueOf(list.get(i).getOptype());
                optypelist = optype.split(",");
                orderAllListRes1.setId(list.get(i).getId());
                orderAllListRes1.setBoxno(boxnolist);
                orderAllListRes1.setOptype(optypelist);
                orderAllListRes1.setOpid(list.get(i).getOpid());
                orderAllListRes1.setOrderno(list.get(i).getOrderno());
                orderAllListRes1.setSmartno(list.get(i).getSmartno());
                orderAllListRes.add(i, orderAllListRes1);
            }
        }else if (roleflag==1){
            String place = null;
            if (!province.isEmpty()){
                place = province;
            }
            if (!city.isEmpty()){
                place =place+"/"+ city;
            }
            if (!area.isEmpty()){
                place = place+"/"+area;
            }
            List<OrderRes> list = orderDao.getOrderListRole(place);
            String[] boxnolist = new String[list.size()];
            String[] optypelist = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                String boxno = list.get(i).getBoxno();
                boxnolist = boxno.split(",");
                String optype = String.valueOf(list.get(i).getOptype());
                optypelist = optype.split(",");
                orderAllListRes1.setId(list.get(i).getId());
                orderAllListRes1.setBoxno(boxnolist);
                orderAllListRes1.setOptype(optypelist);
                orderAllListRes1.setOpid(list.get(i).getOpid());
                orderAllListRes1.setOrderno(list.get(i).getOrderno());
                orderAllListRes1.setSmartno(list.get(i).getSmartno());
                orderAllListRes.add(i, orderAllListRes1);
            }
        }
        Response<PageInfo<OrderAllListRes>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(orderAllListRes);
        response.setOK();
        response.setData(pageInfo);
        return response;
    }

    @Override
    public Response<OrderListRes> getOrderDeatilList(String orderno) {
        List<String> ids = orderDao.getIdByOrderno(orderno);
        if (ids.size()==0){
            return new Response<>(CommonEnum.ORDERNO_NOT_EXIST);
        }
        OrderListRes orderListRes = new OrderListRes();
        List<OrderRes> inlist = orderDao.getOrderDetailByOpType(orderno,0);
        List<OrderRes> outlist = orderDao.getOrderDetailByOpType(orderno,1);
        orderListRes.setOrderInRes(inlist);
        orderListRes.setOrderOutRes(outlist);
        Response<OrderListRes> resResponse = new Response<>();
        resResponse.setOK();
        resResponse.setData(orderListRes);
        return resResponse;
    }

    @Override
    public Response update(String id,int opresult,String remark) {
        if (opresult==1){
            orderDao.updated(id,opresult,remark,new Date());
        }else if (opresult==0){
            orderDao.updated(id,opresult,remark,null);
        }
       Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response<PageInfo<OrderAllListRes>> getTabList(String searchtext, String starttime, String endtime, int roleflag, String province, String city, String area, int pageNo, int pageSize) throws ParseException {
        Page page = PageHelper.startPage(pageNo, pageSize);
        List<OrderAllListRes> orderAllListRes = new LinkedList<>();
        List<OrderRes> list = new LinkedList<>();
        if (roleflag == 0){
            if ("".equals(searchtext) && "".equals(starttime) && "".equals(endtime)) {
                list = orderDao.getOrderList();
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    if (null!=list.get(i).getBoxno()) {
                        String boxno = list.get(i).getBoxno();
                        boxnolist = boxno.split(",");
                    }
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if ("".equals(searchtext) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByTime(datestart, dateend);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (!"".equals(searchtext) && "".equals(starttime) && "".equals(endtime)) {
                list = orderDao.getOrderListBySearch(searchtext);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext == null && starttime == null && endtime == null) {
                list = orderDao.getOrderList();
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext == null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByTime(datestart, dateend);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext != null && starttime == null && endtime == null) {
                list = orderDao.getOrderListBySearch(searchtext);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext != null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByAll(searchtext, datestart, dateend);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (!"".equals(searchtext) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByAll(searchtext, datestart, dateend);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            }
    }else if (roleflag==1){
            String place = null;
            if (!province.isEmpty()){
                place = province;
            }
            if (!city.isEmpty()){
                place =place+"/"+ city;
            }
            if (!area.isEmpty()){
                place = place+"/"+area;
            }
            if ("".equals(searchtext) && "".equals(starttime) && "".equals(endtime)) {
                list = orderDao.getOrderListRole(place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if ("".equals(searchtext) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByTimeRole(datestart, dateend,place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (!"".equals(searchtext) && "".equals(starttime) && "".equals(endtime)) {
                list = orderDao.getOrderListBySearchRole(searchtext,place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext == null && starttime == null && endtime == null) {
                list = orderDao.getOrderListRole(place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext == null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByTimeRole(datestart, dateend,place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext != null && starttime == null && endtime == null) {
                list = orderDao.getOrderListBySearchRole(searchtext,place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (searchtext != null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByAll(searchtext, datestart, dateend);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            } else if (!"".equals(searchtext) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = orderDao.getOrderListByAllRole(searchtext, datestart, dateend,place);
                String[] boxnolist = new String[list.size()];
                String[] optypelist = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    OrderAllListRes orderAllListRes1 = new OrderAllListRes();
                    String boxno = list.get(i).getBoxno();
                    boxnolist = boxno.split(",");
                    String optype = String.valueOf(list.get(i).getOptype());
                    optypelist = optype.split(",");
                    orderAllListRes1.setId(list.get(i).getId());
                    orderAllListRes1.setBoxno(boxnolist);
                    orderAllListRes1.setOptype(optypelist);
                    orderAllListRes1.setOpid(list.get(i).getOpid());
                    orderAllListRes1.setOrderno(list.get(i).getOrderno());
                    orderAllListRes1.setSmartno(list.get(i).getSmartno());
                    orderAllListRes.add(i, orderAllListRes1);
                }
            }
        }
        Response<PageInfo<OrderAllListRes>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setOK();
        response.setData(pageInfo);
        return response;
    }

    @Override
    public Response closeDoor(String id,String username,String smartno, String boxno, String orderno) {
        Response response1 = new Response();

        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        InvokeThingServiceRequest request = new InvokeThingServiceRequest();
        request.setProductKey(ProductKey);
        request.setDeviceName(smartno);
        String param = "{\"DeviceNo\":\""+smartno+"\",\"BoxNo\":\""+boxno+"\",\"OrderNo\":\""+orderno+"\"}";
        request.setArgs(param);
        request.setIdentifier("RemoteOpen");
//        if (orderDao.getBoxno(boxno)==null){
//            return new Response(CommonEnum.BOX_IS_NOT_EXIST);
//        }
        if (orderDao.getSmartsno(smartno)==null){
            return new Response(CommonEnum.SMART_IS_NOT_EXIST);
        }
//        request.setArgs("{}");
//        request.setTopicFullName("/sys/a12bM7wcGVy/Oufu001/thing/event/property/post_reply");
        try
        {
            InvokeThingServiceResponse response = client.getAcsResponse(request);
//            System.out.println(response.getData());
            if (null!=response.getData().getResult()){
            String result = response.getData().getResult();
            JSONObject json = JSONObject.fromObject(result);
            String message = json.getString("ErrorMessage");
//            System.out.println(response.getData().getResult());
//            if (response.getSuccess()==false){
//                response1.setMsg("远程开门失败");
                response1.setData(message);
                response1.setOK();

            }else {
                response1.setError(600);
                response1.setData(response.getErrorMessage());
            }
                return response1;

//            }
//            System.out.println(response.getData().getResult());
//            System.out.println(response.getSuccess());
//            System.out.println(response.getErrorMessage());

        }
        catch (ServerException e)
        {
            e.printStackTrace();
        }
        catch (ClientException e)
        {
            e.printStackTrace();
        }
        OrderRes orderRes = orderDao.getOrderResById(id);
        //添加远程开门到操作记录表
        long opids = DBUtils.nextId();
        String realopid = String.valueOf(opids);
        Date optime = new Date();
        orderDao.insertuporder(realopid,orderRes.getSmartno(),orderRes.getBoxno(),username,optime,2,orderRes.getOrderno(),new Date());
        //添加远程开门到操作记录详情表
        orderDao.insertuporderdetail(String.valueOf(DBUtils.nextId()),realopid,orderRes.getSmartno(),orderRes.getBoxno(),optime,2,new Date());
        return  response1;
    }
}
